package com.hongxun.design.activitys;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.adapters.ExperienceAdapter;
import com.hongxun.design.bean.ExperienceBean;
import com.hongxun.design.interfaces.HeadSecond;
import com.hongxun.design.listeners.ListenerSet;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.IntentToActivity;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.ui.headandbottonpanel.HeadSecondControlPanel;
import com.hongxun.design.ui.pickerview.lib.DensityUtil;
import com.hongxun.design.ui.ui_listview.Swipemenulistview.SwipeMenu;
import com.hongxun.design.ui.ui_listview.Swipemenulistview.SwipeMenuCreator;
import com.hongxun.design.ui.ui_listview.Swipemenulistview.SwipeMenuItem;
import com.hongxun.design.ui.ui_listview.Swipemenulistview.SwipeMenuListView;
import com.hongxun.design.ui.ui_listview.refreshlist.SwipeRefreshListView;
import com.hongxun.design.utils.DataUtil;
import com.hongxun.design.utils.fastjson.FastJsonUtil;
import com.hongxun.design.utils.httputil.HttpStringUtil;

import java.util.List;

/**
 * Created by zerdoor_pc on 2016/7/11.
 * 我的经验
 */
public class ExperienceActivity extends BaseActivity implements HeadSecond, SwipeRefreshLayout.OnRefreshListener {
    String FileTag = "ExperienceActivity";
    SwipeRefreshListView experience_list;
    int startNum = 0;
    int addNum =  MyApp.addNum;
    private Handler delExpHandler, experienceListHandler;
    List<ExperienceBean> data;
    ExperienceAdapter mAdapter;
    int mposition = -1;//删除位置
    EditText serch_words;
    String name = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_experience);
        findBar();
        findView();
    }

    @Override
    public void findBar() {
        HeadSecondControlPanel headsecond_layout = (HeadSecondControlPanel) findViewById(R.id.experience_layout);
        headsecond_layout.initHeadPanel(DataUtil.getsecondbean("", View.INVISIBLE, R.color.bar_right, ListenerSet.back, "我的经验", R.color.title, View.VISIBLE, IconSourse.BACK + "", View.VISIBLE));
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void findView() {
        super.findView();
        name="";
        serch_words = (EditText) findViewById(R.id.serch_words);
        if (experience_list == null)
            experience_list = (SwipeRefreshListView) findViewById(R.id.experience_list);
        experience_list.setOnRefreshListener(this);
        addDelect();
        addExperienceListHandler();
        data = MyApp.File.LoadFileData_ExperienceList(FileTag);
        if (data.size() > 0) {
            experienceListHandler.sendEmptyMessage(1);
        }
        onRefresh();
    }

    @Override
    public void onRefresh() {
        startNum = 0;
        GetExperienceList(HttpStringUtil.SUCCESSED);
    }

    @Override
    public void addMore() {
        super.addMore();
        GetExperienceList(HttpStringUtil.ADDMROE);
    }


    public void search(View v) {
        name = serch_words.getText().toString();
        onRefresh();
    }

    /**
     * 删除方法*
     */
    private void addDelect() {
        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                SwipeMenuItem deleteItem = new SwipeMenuItem(mActivity);
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
                        0x3F, 0x25)));
                deleteItem.setWidth(DensityUtil.dip2px(mActivity, 90));
                deleteItem.setIcon(R.mipmap.ic_delete);
                menu.addMenuItem(deleteItem);
            }
        };
        experience_list.setMenuCreator(creator, new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public void onMenuItemClick(int position, SwipeMenu menu, int index) {
                DelExp(data.get(position).experience_id);
                mposition = position;
            }
        });

        experience_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                // Bundle bundle = new Bundle();
                //bundle.putString(MString.PATH, JSON.toJSONString(data.get(i)));

                ExperienceBean b = data.get(i);
                Bundle bundle = new Bundle();
                bundle.putString(MString.CODE, b.experience_id);
                bundle.putString(MString.TITLE, b.title);
                bundle.putString(MString.TYPE, MString.EXPERIENCE);
                IntentToActivity.intent(mActivity, EncyclopediasActivity.class, bundle);
            }
        });

        addDelExpHandler();
    }

    /**
     * 数据请求*
     */
    private void GetExperienceList(int code) {
        MyApp.HTTP.GetExperienceList(experienceListHandler, code, name, startNum, startNum + addNum);
    }


    /**
     * 数据删除*
     */
    private void DelExp(String experience_id) {
        MyApp.HTTP.DelExp(delExpHandler, HttpStringUtil.SUCCESSED, experience_id);
    }

    /**
     * 数据删除回调*
     */
    public void addDelExpHandler() {
        delExpHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case HttpStringUtil.SUCCESSED:
                        if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
                            data.remove(mposition);
                            experience_list.update();
                        } else {
                            ToastShow.showShort(mActivity, FastJsonUtil.JsonToCheck_String((String) msg.obj));
                        }
                        break;
                    case HttpStringUtil.ERROR:
                        ToastShow.showShort(mActivity, (String) msg.obj);
                        break;
                }
            }
        };
    }

    /**
     * 数据接收*
     */
    public void addExperienceListHandler() {
        experienceListHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case HttpStringUtil.SUCCESSED:
                        if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
                            if (data == null) {
                                data = FastJsonUtil.getExperienceList((String) msg.obj);
                            } else {
                                data.clear();
                                data.addAll(FastJsonUtil.getExperienceList((String) msg.obj));
                            }

                            MyApp.File.SaveFileData(FileTag, "{\"experienceList\":" + JSON.toJSONString(data) + "}");
                            startNum += addNum;
                            experienceListHandler.sendEmptyMessage(1);
                        } else {
                            ToastShow.showShort(mActivity, FastJsonUtil.JsonToCheck_String((String) msg.obj));
                        }
                        break;

                    case 1:
                        if (data == null) return;
                        if (mAdapter == null) {
                            mAdapter = new ExperienceAdapter(mActivity, data);
                            experience_list.setAdapter(mAdapter);
                            experience_list.setFragment(baseActivity);
                            experience_list.setCanScroll(true);
                            experience_list.setDivider(1);
                            experience_list.setRefreshing(false);
                        } else {
                            experience_list.update();
                            experience_list.setRefreshing(false);
                            experience_list.removeFooterView();
                        }
                        break;

                    case HttpStringUtil.ADDMROE:
                        int i = data.size();
                        data.addAll(FastJsonUtil.getExperienceList((String) msg.obj));
                        if (i == data.size()) {
                            ToastShow.showShort(mActivity, "没有数据了！");
                        } else {
                            startNum += addNum;
                        }
                        experienceListHandler.sendEmptyMessage(1);
                        break;
                    case HttpStringUtil.ERROR:
                        experience_list.setRefreshing(false);
                        experience_list.removeFooterView();
                        ToastShow.showShort(mActivity, (String) msg.obj);
                        break;
                }
            }
        };
    }
}
